//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#include "UI/Loaders/UILoader.h"
#include "UI/MetaData/Meta.Widgets.h"
#include "Shared/Resources/ResourceFileInfo.h"

namespace Engine
{
namespace UI
{
	
/*
=================================================
	constructor
=================================================
*/
	UILoader::UILoader ()
	{
	}
		
/*
=================================================
	Register
=================================================
*/
	void UILoader::Register (ResourceManager *rm)
	{
		rm->RegisterLoader( EResource::UI_FRAME,			LoadFrame,		null, this );
		rm->RegisterLoader( EResource::UI_LOCALE,			LoadLocale,		null, this );
		rm->RegisterLoader( EResource::UI_LOCALE_SETTINGS,	LoadLSettings,	null, this );
	}
	
/*
=================================================
	Unregister
=================================================
*/
	void UILoader::Unregister (ResourceManager *rm)
	{
		rm->UnregisterLoader( EResource::UI_FRAME,				LoadFrame,		null, this );
		rm->UnregisterLoader( EResource::UI_LOCALE,				LoadLocale,		null, this );
		rm->UnregisterLoader( EResource::UI_LOCALE_SETTINGS,	LoadLSettings,	null, this );
	}

/*
=================================================
	LoadFrame
=================================================
*/
	bool UILoader::LoadFrame (ResourcePtr &resource, const RFilePtr &file, void *parameter,
								const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		CHECK_ERR( loadParams.Is< UIFrameResource::UIFrameLoadParams >() );

		return _LoadFrame( resource, file, parameter, loadParams.To< UIFrameResource::UIFrameLoadParams >(), ess );
	}
	
/*
=================================================
	LoadLocale
=================================================
*/
	bool UILoader::LoadLocale (ResourcePtr &resource, const RFilePtr &file, void *parameter,
								const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		CHECK_ERR( loadParams.Is< UILocaleResource::UILocaleLoadParams >() );

		return _LoadLocale( resource, file, parameter, loadParams.To< UILocaleResource::UILocaleLoadParams >(), ess );
	}
		
/*
=================================================
	LoadLSettings
=================================================
*/
	bool UILoader::LoadLSettings (ResourcePtr &resource, const RFilePtr &file, void *parameter,
									const Resource::ResLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		CHECK_ERR( loadParams.Is< UILocaleSettingsResource::UILSettingsLoadParams >() );

		return _LoadLSettings( resource, file, parameter, loadParams.To< UILocaleSettingsResource::UILSettingsLoadParams >(), ess );
	}

/*
=================================================
	_LoadFrame
=================================================
*/
	bool UILoader::_LoadFrame (ResourcePtr &resource, const RFilePtr &file, void *parameter,
								const UIFrameResource::UIFrameLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		RFilePtr	new_file;
		ess->GetFileSystem()->ReadToMemIfSmall( file, new_file );

		typedef WidgetMsg	WM;

		RESFILE_CHECK_HEADER( new_file, UIFrameFileInfo );

		MetaData::FrameCfg	frame_cfg;
		CHECK_ERR( frame_cfg.Deserialize( new_file ) );


		UIFrameResourcePtr	frame		= UIFrameResource::New( ess );
		FramePtr			wgt_frame	= frame->GetWidget();

		frame_cfg.Init( wgt_frame.ptr(), ess );

		wgt_frame->ProcessMessage( WM( WM::ON_LOAD, WM::EmptyMessage() ) );

		resource = frame;
		return true;
	}
	
/*
=================================================
	_LoadLocale
=================================================
*/
	bool UILoader::_LoadLocale (ResourcePtr &resource, const RFilePtr &file, void *parameter,
								 const UILocaleResource::UILocaleLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		RFilePtr	new_file;
		ess->GetFileSystem()->ReadToMemIfSmall( file, new_file );

		RESFILE_CHECK_HEADER( new_file, UILocaleFileInfo );

		MetaData::UILocaleCfg	locale_cfg;
		CHECK_ERR( locale_cfg.Deserialize( new_file ) );

		UILocaleResourcePtr	locale = UILocaleResource::New( ess );

		locale_cfg.Init( locale.ptr(), ess );

		resource = locale;
		return true;
	}
		
/*
=================================================
	_LoadLSettings
=================================================
*/
	bool UILoader::_LoadLSettings (ResourcePtr &resource, const RFilePtr &file, void *parameter,
									const UILocaleSettingsResource::UILSettingsLoadParams &loadParams, const EngineSubSystemsRef ess)
	{
		RFilePtr	new_file;
		ess->GetFileSystem()->ReadToMemIfSmall( file, new_file );

		RESFILE_CHECK_HEADER( new_file, UILSettingsFileInfo );

		MetaData::UILocaleSettingsCfg	settings_cfg;
		CHECK_ERR( settings_cfg.Deserialize( new_file ) );

		UILocaleSettingsResourcePtr		lsettings = UILocaleSettingsResource::New( ess );

		settings_cfg.Init( lsettings.ptr(), ess );

		resource = lsettings;
		return true;
	}


}	// UI
}	// Engine